package mrp.beans.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import org.primefaces.component.dialog.Dialog;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;

public class JsfUtil {

    public JsfUtil() {
    }
    
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "---");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg = ex.getLocalizedMessage();
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }

    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
    }

    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {
        String theId = JsfUtil.getRequestParameter(requestParameterName);
        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
    }
    public static void mostrarMensaje(String tipoMensaje,String mensaje, String detalle)
    {
        if(tipoMensaje.equals("info"))
        {
            FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO, mensaje,detalle) );   
        }else{
            if(tipoMensaje.equals("error"))
            {
                FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, mensaje,detalle) );   
            }else{
                if(tipoMensaje.equals("fatal"))
                {
                    FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_FATAL, mensaje,detalle) );   
                }else{
                    if(tipoMensaje.equals("alerta"))
                    {
                        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN, mensaje,detalle) );   
                    }
                }
            }
        }
    }
    public static void mostrarDialogo(String form, String componente, boolean visible)
   {
       javax.faces.context.FacesContext facesContext = javax.faces.context.FacesContext.getCurrentInstance();
       javax.faces.component.UIViewRoot root = facesContext.getViewRoot();
       Dialog dialogo=(Dialog)root.findComponent(form).findComponent(componente);
       dialogo.setVisible(visible);
   }
    public static void mostrarMensajeJS(String mensaje) 
   {
       javax.faces.context.FacesContext fa= FacesContext.getCurrentInstance();
       
        RequestContext context = RequestContext.getCurrentInstance();                            
//        execute javascript oncomplete
        context.execute("window.alert ('"+mensaje+"');");                 
    }
    public static void updateComponente(String form,String componente)
   { 
       try{
        RequestContext context = RequestContext.getCurrentInstance();
        //execute javascript oncomplete
        //context.execute("PrimeFaces.info('Hello from the Backing Bean');");

            //update panel
        System.out.println(" --------^^^^^^^`` form= "+form+" context = "+context.toString());
        context.update(form+componente);

            //scroll to panel
            //context.scrollTo("form:panel")

            //add facesmessage
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Success", "Success"));
       }
       catch(Exception e)
       {
           System.out.println("error en metodo updateComponente: "+e);
       }
   }
    public static void updateComponenteJSF(String form)
    {
       javax.faces.context.FacesContext facesContext = javax.faces.context.FacesContext.getCurrentInstance();
       javax.faces.component.UIViewRoot root = facesContext.getViewRoot();
       javax.faces.component.html.HtmlForm formjsf=(javax.faces.component.html.HtmlForm)root.findComponent(form);
       formjsf.processUpdates(facesContext);              
    }
    public static void redirect(String path)
    {
         javax.faces.context.FacesContext facesContext = javax.faces.context.FacesContext.getCurrentInstance();
         try{
            facesContext.getExternalContext().redirect(path);
         }
         catch(Exception e)
         {
             System.out.println("No se pudo redirigir pagina metodo JsfUtil.redirect(String path)");
         }
    }
    public static void refreshElementJS(String sentenciaJS)
    {
        try{
        RequestContext context = RequestContext.getCurrentInstance();
        //execute javascript oncomplete
//        context.execute("document.getElementById('"+element+"').refresh();");
        context.execute(sentenciaJS);

            //update panel
        System.out.println(" --------^^^^^^^`` refrescando elemento= "+" context = "+context.toString());
//        context.update(form+componente);

            //scroll to panel
            //context.scrollTo("form:panel")

            //add facesmessage
            //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Success", "Success"));
       }
       catch(Exception e)
       {
           System.out.println("error en metodo refreshElementJS: "+e);
       }
//        document.getElementById("inventory").reload();
    }
    //--gestion y control de fechas
    public static String convertirFecha(Date fecha)
    {
        SimpleDateFormat formatofecha = new SimpleDateFormat("yyyy-MM-dd");
        return formatofecha.format(fecha);
        
    }
    public static Date convertirAFecha(String fecha)
    {
        SimpleDateFormat formatofecha = new SimpleDateFormat("yyyy-MM-dd");
                    
        Date fechaDate = null;
        try 
        {
            fechaDate = formatofecha.parse(fecha);
        } 
        catch (java.text.ParseException ex) 
        {
            ex.printStackTrace();
        }
        return fechaDate;     
    }
    public static boolean verficaFechaMenorFechaActual(String fechaIngresada)
    {
        String fechaSistema= convertirFecha(new Date());
        int anioIngresado=Integer.parseInt(fechaIngresada.substring(0,4));
        int anioSistema=Integer.parseInt(fechaSistema.substring(0, 4));
        int mesIngresado=Integer.parseInt(fechaIngresada.substring(5,7));
        int mesSistema=Integer.parseInt(fechaSistema.substring(5, 7));
        int diaIngresado=Integer.parseInt(fechaIngresada.substring(8,10));
        int diaSistema=Integer.parseInt(fechaSistema.substring(8,10));
        
        if(anioIngresado<=anioSistema)
        {
            if(mesIngresado<mesSistema)
            {
                return true;
            }
            else
            {
                if((mesIngresado==mesSistema)&&(diaIngresado<=diaSistema))
                {
                    return true;
                }
                else
                {
                    return false;
                }                
            }
        }
        else
        {
            return false;
        }
    }
    //--fin gestion y control de fechas
    
    //--validar cedula y ruc
    public static boolean validarCedula(String NumeroCedula)
    {
        return true;
    }
    public static boolean validarRuc(String NumeroRuc)
    {
        return true;
    }
    //--fin validar cedula y ruc
    
    //--obtener y setear value y label del selectonemenu javax.faces.component.html.HtmlSelectOneMenu
    public static void  corregirSelectOneMenu(javax.faces.component.html.HtmlSelectOneMenu som)
    {
        if(som.getChildren().size()>1)
        {
            System.out.println(",,hm.getChildren().size() = "+ som.getChildren().size());
            for(int i=som.getChildren().size()-1;0<i;i=i-1)
            {
                System.out.println(",,componente = "+ som.getChildren().get(i));
                som.getChildren().remove(i);
            }
        }
//        return som;
    }
    public static String[] getValueYLabelSelectOneMenu(String valor)
    {
        String [] retorno=new String[2];
        for(int i=0;i<valor.length();i++)
        {
            if(valor.substring(i,i+1).equals("|"))
            {                         
                retorno[0]=valor.substring(0,i);  
                retorno[1]=valor.substring(i+1);
                break;
            }
        }
        return retorno;
    }
    public static String setValueYLabelSelectOneMenu(String value,String label)
    {
        
        return value+"|"+label;
    }
    //--fin obtener y setear value y label del selectonemenu
    
    //imagen no disponible cadena de bytes
    public static byte[] getImagenNoDisponible()
    {
        byte[] b={-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,1,0,72,0,72,0,0,-1,-37,0,67,0,5,3,4,4,4,3,5,4,4,4,5,5,5,6,7,12,8,7,7,7,7,15,11,11,9,12,17,15,18,18,17,15,17,17,19,22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31,19,23,34,36,34,30,36,28,30,31,30,-1,-37,0,67,1,5,5,5,7,6,7,14,8,8,14,30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,-1,-64,0,17,8,0,75,0,75,3,1,34,0,2,17,1,3,17,1,-1,-60,0,26,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,6,7,5,8,3,4,-1,-60,0,43,16,0,1,4,2,1,4,1,4,2,2,3,0,0,0,0,0,1,2,3,4,5,6,17,0,7,18,19,33,20,34,35,49,65,50,51,21,36,23,81,113,-1,-60,0,20,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-60,0,20,17,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-10,95,28,113,-64,113,-57,28,7,28,113,-64,113,-57,28,7,28,113,-64,-28,102,-103,4,44,83,18,-75,-55,-84,90,-112,-20,58,-72,-114,75,125,12,36,41,-59,33,9,42,33,32,-112,9,-48,-3,-111,-55,106,-66,-83,-29,22,24,-82,77,-112,-73,26,-47,-74,113,-39,-113,67,-109,29,-58,80,30,125,-58,-42,91,30,20,-123,-112,-96,-29,-128,-95,27,35,106,4,29,107,-99,46,-76,83,89,100,61,37,-54,-24,-87,-29,124,-101,25,-11,50,35,-58,103,-67,40,-14,56,-90,-56,74,123,-108,66,70,-55,-4,-110,7,32,49,-114,-109,-37,120,33,77,-80,123,-32,41,-85,11,39,-20,43,-76,-105,62,122,68,-7,82,107,-43,-34,-107,105,61,-118,127,-55,-5,59,32,29,16,120,21,-49,-11,107,23,98,54,20,-5,-84,-39,37,57,123,12,-56,-121,-10,82,126,43,78,-8,-126,23,35,74,-46,1,91,-19,55,-79,-35,-11,44,15,-57,-66,117,46,51,102,-94,-33,-55,-94,-88,-57,-18,-78,43,8,104,66,-26,-73,92,-106,18,-104,-95,99,104,11,91,-18,-74,-114,-30,61,-10,-126,85,-94,9,0,17,-68,99,21,-23,47,81,-18,48,-71,48,111,36,83,-29,-18,47,27,-81,-57,99,-57,-103,20,-52,122,59,113,-104,65,47,54,-29,50,18,-124,40,-56,43,80,-40,81,-5,77,-97,-42,-71,97,-118,65,-50,106,34,-37,-65,127,-121,89,89,28,-99,49,-27,-49,69,61,-93,12,74,-121,49,49,-102,-114,-14,123,-53,-51,15,25,-16,37,104,91,110,119,14,-14,8,26,-32,83,35,-87,14,73,-70,122,-102,-77,3,-53,39,-50,-115,13,-103,114,-102,74,97,-80,99,-91,-43,-70,-124,37,94,121,13,-19,68,-80,-25,-15,-18,26,-47,4,-126,57,-9,-58,-6,-107,71,119,-107,-33,-30,-23,-121,101,6,-42,-119,-92,57,37,-103,77,-74,3,-128,-74,-121,22,27,82,22,-96,-78,-113,43,97,94,-3,21,-89,91,4,30,101,56,-106,17,-105,-44,-28,51,-84,50,-100,103,58,-70,18,-96,-74,-36,119,106,114,-33,19,-83,4,-51,-100,-22,90,125,-59,77,101,110,-108,50,-5,9,4,-107,-127,-91,0,127,103,-69,35,3,-55,-43,11,38,-56,-86,-22,-99,-121,-112,51,122,45,41,24,-105,45,-75,-71,38,58,-85,-94,48,-12,103,92,11,80,30,79,27,-120,37,74,58,90,16,-67,-112,1,33,93,119,-43,-68,98,-101,17,-60,114,-103,-15,-20,-37,-83,-54,75,31,21,65,-108,21,70,67,-84,-105,-4,-113,-114,-1,0,-91,8,64,37,69,37,90,-47,-4,-113,124,-24,100,61,69,-94,-92,-69,-97,77,41,-117,7,103,68,110,17,109,-106,25,11,84,-73,37,-87,-12,-76,-45,35,-69,101,127,-21,-72,79,119,106,64,-10,78,-127,34,74,-73,6,-72,119,15,-24,-3,45,-83,67,110,-73,71,25,44,94,-80,-29,-83,-87,45,36,-44,63,25,105,63,86,-106,11,-114,4,125,61,-33,-99,-2,54,121,43,73,-45,-98,-95,84,103,82,46,-105,9,-69,54,-79,-89,-21,-111,72,-29,-77,16,21,109,13,-90,-20,26,82,20,73,-38,30,67,83,80,-98,-27,-128,20,-74,-9,-67,40,-112,27,30,49,-105,-58,-71,-73,-107,73,42,-90,-50,-114,-30,51,41,-112,-72,54,41,107,-56,-90,84,74,67,-120,83,75,91,107,78,-63,7,-75,68,-92,-21,96,108,110,-109,-112,-104,-12,11,-37,-98,-91,12,-54,-38,-115,-6,8,-80,-22,28,-84,-119,18,83,-20,-71,33,-27,58,-13,110,56,-30,-4,43,90,18,-111,-31,108,36,5,-110,118,-94,117,-21,119,124,7,28,-102,-22,70,77,43,20,-57,-103,-80,-123,90,-51,-108,-87,22,48,-21,-39,97,-23,70,59,101,114,100,33,-108,-87,75,8,89,0,23,1,58,73,58,28,-108,-51,58,-95,113,-125,-42,77,127,44,-60,-93,49,33,53,-46,-90,87,-104,54,-90,68,105,107,-114,-47,117,108,23,20,-54,22,-38,-54,18,-91,13,-74,65,9,87,-67,-115,16,-44,56,-26,125,-114,-25,-41,25,12,-37,39,-22,-79,-6,-57,41,-21,-89,73,-123,33,-41,46,59,102,-91,76,45,72,90,-52,96,-55,9,73,82,14,-69,-100,10,41,32,-21,-34,-71,-59,-86,-21,12,-90,41,-79,-37,-20,-69,30,-127,79,81,-112,85,57,103,21,-24,118,-54,-104,-29,45,-73,20,-54,80,121,-75,48,-47,79,-37,73,27,73,88,-18,-48,-3,-125,-64,-42,-8,-26,118,-82,-96,95,86,-77,89,99,-109,97,-56,-86,-87,-75,117,12,70,117,22,94,103,-40,117,-63,-10,-111,37,-81,26,67,125,-25,72,-38,22,-32,10,80,7,-2,-7,-57,-57,58,-42,-51,-57,76,33,101,-54,-57,87,18,123,-74,53,-16,-92,-43,57,51,106,97,51,28,104,52,-24,115,-77,-21,73,105,-28,56,62,-111,-65,105,-12,65,32,53,-50,57,-119,-47,-11,-70,-22,-45,8,-97,-100,39,11,-81,110,-122,-79,17,85,55,119,-118,50,-2,-20,72,-14,72,105,-81,-114,16,-31,72,-110,-108,-128,92,73,81,73,-48,27,3,-107,-49,117,27,-57,-46,27,-2,-96,127,-122,-33,-8,-125,101,-2,-105,-55,-2,-33,-122,-5,-52,-1,0,103,103,-45,-33,-30,-33,-15,61,-67,-38,-9,-83,-112,-66,-29,-100,44,110,94,95,34,67,-125,34,-93,-94,-82,-113,-29,-37,107,-127,112,-20,-75,-87,91,30,-118,87,25,-96,6,-73,-17,103,-1,0,63,124,-18,-16,36,-70,-81,65,111,-111,99,49,98,-47,-120,42,-99,18,-38,5,-118,17,49,-11,50,-45,-126,52,-90,-34,40,43,74,22,83,-80,-39,0,-10,-97,103,-111,-67,78,-64,115,110,-92,82,-54,98,-28,-29,-11,14,71,-82,-102,-43,100,72,-77,30,-110,-123,74,-111,29,108,7,94,121,77,32,-124,37,14,44,118,-91,-78,118,-83,-17,-48,28,-41,-8,-32,100,-71,95,79,-17,-14,60,-102,29,-118,-24,-80,-6,-103,49,44,17,32,95,-61,-112,-23,-80,121,-124,43,-6,20,-113,10,116,28,78,-112,-67,-70,-95,-94,116,-109,-21,95,-73,19,-24,-2,53,93,-46,38,-16,-7,84,-108,-80,-19,37,-29,-55,-87,-75,-79,-83,-120,-124,56,-14,-53,1,-73,23,-28,-19,74,-105,-75,109,64,-85,-39,58,36,115,77,-29,-127,-103,89,-30,-39,-18,79,6,-90,-105,41,123,31,106,5,108,-106,102,61,46,19,-18,-83,-21,7,88,61,-52,-19,-91,54,-108,-80,-97,34,80,-75,105,110,127,29,15,-50,-7,49,115,-47,-100,-123,-52,91,1,-117,83,101,87,30,-58,-94,45,68,28,-127,43,91,-98,9,-116,-62,113,-89,82,-90,-56,70,-53,-120,91,107,8,-18,3,105,117,96,-21,-41,55,78,56,30,108,-57,-70,17,-110,-41,-29,18,41,36,-45,96,-18,-39,-68,-104,34,62,77,-14,92,51,-21,20,-60,56,-84,21,48,-97,-116,9,41,91,11,90,126,-22,1,-17,-10,7,-80,105,-20,48,-114,-91,-85,-91,-39,47,79,98,-42,98,43,-117,108,-27,-89,-122,-63,-53,-55,8,113,9,-105,33,-25,-110,84,-56,-120,70,-45,-27,0,-128,-77,-8,-4,-13,108,-29,-127,13,-45,-116,101,-54,27,25,14,127,-57,120,54,46,-121,89,-19,50,40,-92,-7,29,116,-126,52,-107,-113,-120,-49,-45,-7,59,-18,62,-64,-11,-17,98,-25,-114,56,14,56,-29,-128,-29,-114,56,14,56,-29,-128,-29,-114,56,14,56,-29,-127,-1,-39};
        return b;        
    }
    //retorna imagen para mostrar
    public static DefaultStreamedContent convertirByteEnImagen(byte[] imagen)
   {
       InputStream is=new ByteArrayInputStream(imagen);
       DefaultStreamedContent dmcImagen=new DefaultStreamedContent(is,"image/jpg");
       return dmcImagen;
//       System.out.println("toy en metodo imagen-.................................");
   }
}